/*
 * Copyright (c) 2008 Anshuman Manur <anshuman.manur@gmail.com>,
 * Lavanya Shastri <lavanya.shastri@gmail.com>
 * 
 * This file is part of Andronova.
 * 
 * Andronova is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Andronova is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with Andronova.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * This file may use the functionality provided by the library
 * jid3lib version 0.5.4 (<http://sourceforge.net/projects/javamusictag/>).
 * jid3lib and its uses are covered by GNU Lesser General Public License.
 * 
 */

package com.googlecode.andronova;

import java.io.InputStream;
import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.farng.mp3.id3.AbstractID3v2;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.Menu.Item;
import android.widget.TextView;

import com.googlecode.andronova.helper.MyHandler;
import com.googlecode.andronova.helper.ParsedDataSet;
import com.googlecode.andronova.helper.Song;

public class Lyrics extends Activity {

	private static final String LYRICSFLY_KEY = "cb49e4291347aa894";
	private static final String LYRICSFLY_URL = "http://lyricsfly.com/api/api.php?";
	
	private ProgressDialog pDialog;
	private Handler mHandler;
	private Context ctx;
	
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.lyric_screen);
		
		ctx = this;
		mHandler = new Handler();		
		final TextView tv = (TextView) findViewById(R.id.lyric_main_tv);
		
		if(Playback.pbOrganiser == null) {
			tv.setText("No Song Playing");
			return;
		}
		
		new Thread(new Runnable() {
			public void run() {
				
				mHandler.post(new Runnable() {
					public void run() {
						pDialog = ProgressDialog.show(ctx, "Please Wait", "Fetching Lyrics");
					}
				});
				
				Song s = Playback.pbOrganiser.getCurrentSong();
				AbstractID3v2 tag = s.getID3v2Tag();
				
				String artist, title;
				
				if(tag != null) {
					artist = tag.getLeadArtist();
					title = tag.getSongTitle();
					
					artist = artist.toLowerCase();
					artist = artist.replace(' ', '+');
					
					title = title.toLowerCase();
					title = title.replace(' ', '+');
				}
				else {
					artist = "";
					title = "";
				}
					
				String urlString = LYRICSFLY_URL + "i=" + LYRICSFLY_KEY + "&a=" + artist + "&t=" + title;
				try {
					//Create a URL to load XML data from.
					URL url = new URL(urlString);
					
					//Get a SAXParser from the SAXPArserFactory.
					SAXParserFactory spf = SAXParserFactory.newInstance();
		            SAXParser sp = spf.newSAXParser();

		            //Get the XMLReader of the SAXParser we created.
		            XMLReader xr = sp.getXMLReader();
		            
		            //Create a new ContentHandler and apply it to the XML-Reader.
		            MyHandler myHandler = new MyHandler();
		            xr.setContentHandler(myHandler);
		            
		            //Parse the XML-data from the URL.
		            InputStream iS = url.openStream();
		            InputSource iSou = new InputSource(iS);
		            xr.parse(iSou);
		            //Parsing has finished.
		            
		            //Handler gives us parsed data.
		            final ParsedDataSet myParsedDataSet = myHandler.getParsedDataSet();
		            
		            //Set Content
		            mHandler.post(new Runnable() {
		            	public void run() {
		            		tv.setText(myParsedDataSet.toString());
		            	}
		            });
				}
				catch(final Exception e) {
					mHandler.post(new Runnable() {
						public void run() {
							tv.setText("Error:" + e.toString());
						}
					});
				}
				finally {
					pDialog.dismiss();
				}
			}
		}).start();
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		
		menu.add(0, 0, "Done", R.drawable.andro_done);
		
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(Item item) {
		super.onOptionsItemSelected(item);
		
		if(item.getId()==0) {
			finish();
			return true;
		}
		return false;
	}	
}
